﻿using System.Collections;
using System.Collections.Generic;
using Gp.Scripts.Core;
using QFramework;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

namespace Gp.Scripts.GUI
{
    public enum LogType
    {
        All,
        Fight,
        Buff
    }
    public class TextUIController : MonoBehaviour, IController
    {
        [SerializeField] private LogTabButton buttonPrefab;
        [SerializeField] private TMP_Text content;
        [SerializeField] private Transform buttonParent;
        [SerializeField] private GameObject ScrollView;
        
        private readonly List<LogType> _selectedTypes = new();
        private ScrollRect scrollRect;
        private void Start() {
            //初始化tab
            InitTabs();
            content.text = "城主使用了田中脊髓剑\n效果拔群\n \n你结束了回合";
            scrollRect = ScrollView.GetComponent<ScrollRect>();
            this.RegisterEvent<LogChangeEvent>(UpdateLogContent)
                .UnRegisterWhenGameObjectDestroyed(gameObject);
        }

        private void UpdateLogContent(LogChangeEvent obj = null) 
        {
            throw new System.NotImplementedException("Not yet implemented");

            // IEnumerable<string> tempStrings = Global.Get<LogSystem>().GetLogList(_selectedTypes);
            var tempContent = "";
            // foreach (var str in tempStrings)
            // {
            //     tempContent += str;
            //     tempContent += "\n \n";
            // }
            //
            // content.text = tempContent;
            // StartCoroutine(SetScrollBar());
        }

        private IEnumerator SetScrollBar()
        {
            yield return new WaitForEndOfFrame();
            scrollRect.verticalNormalizedPosition = 0;
            Canvas.ForceUpdateCanvases();
            // yield return null;
        }
        private void InitTabs()
        {
            IReadOnlyList<string> names = new []{"All","Battle","Buff"};
            foreach (var item in names)
            {
                var button = Instantiate(buttonPrefab, buttonParent);
                button.transform.position += (Vector3.up * 163);
                
                button.tabName.text = item;
                if (string.Equals(item, names[0]))
                {
                    button.Type = LogType.All;
                }
                if (string.Equals(item, names[1]))
                {
                    button.Type = LogType.Fight;
                }
                if (string.Equals(item, names[2]))
                {
                    button.Type = LogType.Buff;
                }

                button.button.onClick.AddListener(() => {
                    Debug.Log("点击tab按钮");
                    foreach (var item1 in _selectedTypes)
                    {
                        Debug.Log(item1);
                    }
                    if (_selectedTypes.Contains(button.Type)){
                        _selectedTypes.Remove(button.Type);
                        button.outLine.SetActive(false);
                    }
                    else{
                        _selectedTypes.Add(button.Type);
                        button.outLine.SetActive(true);
                    }

                    UpdateLogContent();
                });
                
                
                button.outLine.SetActive(string.Equals(item,"All"));
                _selectedTypes.Add(LogType.All);
            }
        }

        public IArchitecture GetArchitecture() {
            return MainArchitecture.Interface;
        }
    }
}